<template>
    <!-- 查询条件 -->
    <el-card>

    </el-card>
    <!-- 我的待办列表 -->
    <el-card>
        <el-table :data="todoList" border stripe>
            <el-table-column label="实例单号" prop="InstanceId" align="center">
            </el-table-column>
            <el-table-column label="申请人" prop="Applicant" align="center">
            </el-table-column>
            <el-table-column label="上一个操作人" prop="PreOperator" align="center">
            </el-table-column>
            <el-table-column label="开始时间" prop="StartTime" align="center">
            </el-table-column>
            <el-table-column label="结束时间" prop="EndTime" align="center">
            </el-table-column>
            <el-table-column label="原因" prop="Reason" align="center">
            </el-table-column>
            <el-table-column label="操作" align="center">
                <template v-slot="scope">
                    <el-link type="success" @click="FlowExamination(scope.row.Id, true)"
                        style="margin-right:10px;">通过</el-link>
                    <el-link type="danger" @click="FlowExamination(scope.row.Id, false)">驳回</el-link>
                </template>
            </el-table-column>
        </el-table>

    </el-card>
    <!-- 分页 -->
    <el-card>

        <table style="margin: 5px auto;">
            <tbody>
                <tr>
                    <td><el-pagination v-model:current-page="pager.pageIndex" v-model:page-size="pager.pageSize"
                            :page-sizes="[1, 2, 3, 10]" layout="total, slot,sizes" :total="pager.totalCount"
                            @size-change="handleSizeChange" @current-change="handleCurrentChange" prev-text="上一页"
                            next-text="下一页">
                            <span class="el-pagination__total is-first">&nbsp;共计{{ pager.pageCount }}页,当前{{
            pager.pageIndex
        }}/{{ pager.pageCount }}页</span>
                        </el-pagination>
                    </td>
                    <td><el-pagination v-model:current-page="pager.pageIndex" v-model:page-size="pager.pageSize"
                            :page-sizes="[1, 2, 3, 10]" layout="slot, prev, pager" :total="pager.totalCount"
                            @size-change="handleSizeChange" @current-change="handleCurrentChange" prev-text="上一页"
                            next-text="下一页">
                            <button @click="handleCurrentChange(1)" :disabled="pager.pageIndex == 1"
                                class="btn-prev">首页</button>
                        </el-pagination></td>
                    <td><el-pagination v-model:current-page="pager.pageIndex" v-model:page-size="pager.pageSize"
                            :page-sizes="[1, 2, 3, 10]" layout="slot, next, jumper" :total="pager.totalCount"
                            @size-change="handleSizeChange" @current-change="handleCurrentChange" prev-text="上一页"
                            next-text="下一页">
                            <button @click="handleCurrentChange(pager.pageCount)"
                                :disabled="pager.pageIndex == pager.pageCount" class="next-prev">尾页</button>
                        </el-pagination></td>
                </tr>
            </tbody>
        </table>
    </el-card>

</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessageBox, ElMessage } from 'element-plus'
import { loginStore } from '@/stores/login';
import axios from 'axios'
import { baseURL_dev } from '@/config/baseURL'

const baseURL = baseURL_dev
axios.defaults.baseURL = baseURL;
const store = loginStore();


//查询条件
const condition = reactive({

})
const pager = reactive({
    pageIndex: 1,
    pageSize: 10,
    totalCount: 0,
    pageCount: 0,
})
//待办列表
const todoList = ref([])
//获取待办列表
async function getTodoList() {
    let roleId = ref(0)
    let empId = store.getUserInfo().IsTenant ? store.getUserInfo().TenantId + 'admin' : store.getUserInfo().UId;
    await axios.get("Employee/GetEmpRoleId?empId=" + empId).then(res => {
        roleId = res.data.Data;
    })
    await axios.get("WorkFlow/GetTodoList", {
        params: {
            roleId: roleId,
            pageIndex: pager.pageIndex,
            pageSize: pager.pageSize,
        }
    }).then(res => {
        console.log(res.data)
        todoList.value = res.data.Data;
        pager.totalCount = res.data.TotalCount;
        pager.pageCount = res.data.PageCount;
    })
}
/* const emits = defineEmits(["todoCount"]);
emits("todoCount", pager.pageCount); */

function handleSizeChange(size) {
    pager.pageSize = size;
    getTodoList();
}
function handleCurrentChange(index) {
    pager.pageIndex = index;
    getTodoList();
}

//流程审批
function FlowExamination(requestId, passed) {
    let passFormat = passed ? '通过' : '驳回';
    ElMessageBox.confirm(`确定${passFormat}吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(() => {
        axios.post(`WorkFlow/FlowExamination?requestId=${requestId}&passed=${passed}`).then(res => {
            if (res.data.Code == 200) {
                ElMessage({
                    type: 'success',
                    message: '审批成功',
                })
                //刷新待办列表
                handleCurrentChange(1);
            }
            else {
                ElMessage({
                    type: 'error',
                    message: res.data.Msg,
                })
            }
        })
    }).catch(() => {
        //取消
    });

}


onMounted(() => {

    getTodoList();
})


</script>

<style scoped></style>